package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.cutdown.CountDownTimerUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.App;
import com.igexin.sdk.PushManager;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by xlm on 2017/11/14 0014
 */

public class ChangeMobileActivity extends BaseActivity {
    @Inject(R.id.back)
    private RelativeLayout mLayoutBack;
    @Inject(R.id.title)
    private TextView mViewTitle;
    @Inject(R.id.change_mobile_mobile)
    private EditText mEditTextMobile;
    @Inject(R.id.change_mobile_yzcode)
    private EditText mEditTextCode;
    @Inject(R.id.change_mobile_password)
    private EditText mEditTextPassword;
    @Inject(R.id.change_mobile_getyzcode)
    private TextView mTextViewSms;
    @Inject(R.id.change_mobile_next)
    private TextView mTextViewConfim;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_change_mobile);
    }

    @Override
    protected void initViews() {
        mViewTitle.setText("修改手机号");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }

    @Override
    protected void onSetListener() {
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTextViewSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSmsData();
            }
        });
        mTextViewConfim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetPassWord();
            }
        });
    }

    /**
     * 重置密码
     */
    private void resetPassWord() {
        final String mobile = mEditTextMobile.getText().toString();
        String yzcode = mEditTextCode.getText().toString();
        final String password = mEditTextPassword.getText().toString();
        if (!CommonUtil.isMobile(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (CommonUtil.isEmpty(yzcode)) {
            showToast("请输入验证码");
            return;
        }
        if (CommonUtil.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (!CommonUtil.isMobile(mobile)) {
            showToast("手机号码不正确");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.MOBILE_CHANGE);
        param.add("mobile", mobile);
        param.add("password", password);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("verify", yzcode);
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API).params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        //修改手机号成功
                        unbindGetui();
                    }
                    showToast(result.getInfo());
                } else {
                    showToast(string);
                }
            }
        });
    }

    /**
     * 解绑个推
     */
    private void unbindGetui() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.UNBIND);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                exitApp();
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                exitApp();
            }
        });
    }

    /**
     * 退出登录
     */
    private void exitApp() {
        PushManager.getInstance().stopService(App.getApp());//关闭个推sdk
        Intent intent = new Intent(getThis(), LoginModeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        App.getApp().cancelAccount();//注销
        startActivity(intent);
    }


    /**
     * 获取短信验证码
     */
    private void getSmsData() {
        String mobile = mEditTextMobile.getText().toString();
        if (!CommonUtil.isMobile(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.SENDSMS);
        param.add("mobile", mobile);
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API).params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    try {
                        showToast(new JSONObject(string).optString("info"));
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                new CountDownTimerUtil(mTextViewSms, 30000, 1000).start();
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }
}
